package com.foreveross.action;

import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.common.Exception.ExceptionConstant;
import com.foreveross.service.HrNewsInforService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.FileUtil;
import com.foreveross.util.StringUtil;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

/**
 * 楼盘资讯 管理
 * 
 * @author zw
 * 
 */
@Controller
@RequestMapping(value = "/sysHrNewsInfoController")
public class SysHrNewsInfoController  extends BaseCostroller {
	private Logger log = Logger.getLogger(SysHrNewsInfoController.class);

	@Resource
	HrNewsInforService hrNewsInforService;

	@Resource
	private LogService logService;

	/**
	 * 获取资讯内容列表
	 * 
	 * @param request
	 * @param response
	 * @param title
	 * @param publisher
	 * @param startTime
	 * @param endTime
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrNewsInfoList")
	public void getHrNewsInfoList(HttpServletRequest request,
			HttpServletResponse response, String title, String houseId,
			String publisher, String startTime, String endTime,String type, String page,
			String rows,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) hrNewsInforService.findCodeListCount(title,
					houseId, publisher, startTime, endTime,type,userId);
			PageVo<HrNewsInfo> pageObj = new PageVo<HrNewsInfo>(page,
					countRecord, rows);
			pageObj.setRows(hrNewsInforService.findCodeList(title, houseId,
					publisher, startTime, endTime,type, pageObj.getpageIndex(),
					pageObj.getpageSize(),userId));
			resultVo.setSuccess(true);
			resultVo.setData(pageObj);
			log.info("获取资讯内容列表  查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("获取资讯内容列表 查看失败");
			log.error("获取资讯内容列表  查看失败!", e);
		}
		//List list = Arrays.asList("houseId", "type","newsHouses");
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
		
	}
	
	
	/**
	 * 
	 * 查看资讯信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findHrNewsInfo")
	public void findHrNewsInfo(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrNewsInfo hrNewsInfo = (HrNewsInfo) this.findObject(HrNewsInfo.class, id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, hrNewsInfo.getImage());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(hrNewsInfo);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看资讯信息失败");
		}
		//List list = Arrays.asList("type", "houseId", "sysUserByCreateId",
				//"sysUserByPublisherId","newsHouses");
		//CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);

	}

	/**
	 * 删除上传文件
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteFileList")
	public void deleteFileList(HttpServletRequest request,
			HttpServletResponse response,String fileList) {
		ResultVo resultVo = new ResultVo();
		try {
			String[] farray = fileList.split(",");
			for (int i = 0; i < farray.length; i++) {
				File file = new File(farray[i]);
				if (file.exists()) {
					file.delete();
				}
			}
			resultVo.setSuccess(true);
			resultVo.setMessage("删除上传文件成功");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("删除上传文件失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 资讯内容 删除
	 * 
	 * @param request
	 * @param response
	 * @param codeIds
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHrNewsInfoList")
	public void deleteHrNewsInfoList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			hrNewsInforService.deleteObjecList("HrNewsInfo", ids);
			resultVo.setSuccess(true);
			log.info("资讯内容 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("资讯内容 删除失败");
			log.info("资讯内容  删除失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 更新楼盘资讯
	 * 
	 * @param request
	 * @param response
	 * @param hrNewsInfo
	 * @param updateType
	 * @param file
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/updateHrNewsInfo")
	public void updateHrNewsInfo(HttpServletRequest request,
			HttpServletResponse response, HrNewsInfo hrNewsInfo,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			if (updateType.equals(Constants.UPDATE_ADD))
				hrNewsInfo.setCreateTime(new Date());
			hrNewsInfo.setContentImgList(CommonUtil.getContentImgList(hrNewsInfo.getContent()));
			
			hrNewsInforService.saveOrUpdate(hrNewsInfo, updateType);
			log.info(Constants.getUpdateType(updateType) + "楼盘资讯 " + updateType
					+ "成功!");
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"楼盘资讯" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "楼盘资讯["
							+ hrNewsInfo.toString() + "]");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage(Constants.getUpdateType(updateType) + "楼盘资讯 "
					+ updateType + "失败");
			log.error(Constants.getUpdateType(updateType) + "楼盘资讯" + updateType
					+ "失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
